#include<iostream>
#include<assert.h>
#include <algorithm>
namespace STL
{
	template<class T>
	struct ListNode
	{
		ListNode(const T& val=T())
			:_val(val)
			,_next(nullptr)
			,_prev(nullptr)
		{}
		T _val;
		ListNode<T>* _next;
		ListNode<T>* _prev;
	};

	template<class T,class Ref,class Ptr>
	struct List_iterator
	{

		typedef ListNode<T> node;
		typedef List_iterator<T, Ref, Ptr> self;
		List_iterator()
			:_pnode(nullptr)
		{}
		List_iterator(node*pnode)
			:_pnode(pnode)
		{}

		self operator++()
		{
			_pnode = _pnode->_next;
			return *this;
		}
		self operator++(int)
		{
			self tmp(*this);
			_pnode = _pnode->_next;
			return tmp;
		}
		self operator--()
		{
			_pnode = _pnode->_prev;
			return *this;
		}
		self operator--(int)
		{
			self tmp(*this);
			_pnode = _pnode->_prev;
			return tmp;
		}
		bool operator==(const self& s)const
		{
			return _pnode == s._pnode;
		}
		bool operator!=(const self& s)const
		{
			return _pnode != s._pnode;
		}
		Ref operator*()
		{
			return _pnode->_val;
		}
		Ptr operator->()
		{
			return &_pnode->_val;
		}
		node* _pnode;
	};
	template<class T>
	class List
	{
	public:
		typedef ListNode<T> node;
		typedef List_iterator<T, T&, T*> iterator;
		typedef List_iterator<T, const T&,const T*> const_iterator;
		List()
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
		}
		List(const List<T>& lt)
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
			for (const auto& e : lt)
			{
				push_back(e);
			}
		}
		List<T>& operator=(const List<T>& lt)
		{
			if (this!=&lt)
			{
				clear();
				for (const auto& e : lt)
				{
					push_back(e);
				}
			}
			return *this;
		}
		~List()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		iterator begin()
		{
			return iterator(_head->_next);
		}
		iterator end()
		{
			return iterator(_head);
		}
		const_iterator begin()const
		{
			return const_iterator(_head->_next);
		}
		const_iterator end()const
		{
			return const_iterator(_head);
		}

		T& front()
		{
			return *begin();
		}
		T& back()
		{
			return *(--end());
		}
		const T& front() const
		{
			return *begin();
		}
		const T& back()const
		{
			return *(--end());
		}

		void insert(iterator pos, const T& val)
		{
			assert(pos._pnode);

			node* cur = pos._pnode;
			node* prev = cur->_prev;
			node* newnode = new node(val);
			newnode->_next = cur;
			newnode->_prev = prev;

			prev->_next = newnode;
			cur->_prev = newnode;
		}
		iterator erase(iterator pos)
		{
			assert(pos._pnode);

			node* cur = pos._pnode;
			node* next = cur->_next;
			node* prev = cur->_prev;
			prev->_next = next;
			next->_prev = prev;

			delete cur;
			cur = nullptr;
			return iterator(next);
		}
		void push_back(const T&val)
		{
			insert(end(), val);
		}
		void pop_back()
		{
			erase(--end());
		}
		void push_front(const T&val)
		{
			insert(begin(), val);
		}
		void pop_front()
		{
			erase(begin());
		}

		size_t size() const
		{
			iterator it = begin();
			size_t sz = 0;
			while (it != end())
			{
				it++;
				sz++;
			}
			return sz;
		}
		void resize(size_t n, const T& val)
		{
			iterator i=begin();
			int len = 0;
			while (len < n && i != end())
			{
				len++;
				i++;
			}
			if (len == n)
			{
				while (i != end)
				{
					i = erase(i);
				}
			}
			else
			{
				while (len < n)
				{
					push_back(val);
					len++;
				}
			}
		}
		bool empty()const
		{
			return begin() == end();
		}
		void swap(List<T>& lt)
		{
			std::swap(_head, lt._head);
		}
		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}
	private:
		node* _head;
	};
}
using namespace STL;
void print(const List<int>& ls)
{
	List<int>::const_iterator it=ls.begin();
	while (it != ls.end())
	{
		std::cout << *it << " ";
		it++;
	}
	std::cout <<std::endl;
}
int main()
{
	List<int> ls;
	ls.push_back(1);
	ls.push_back(2);
	ls.push_back(3);
	ls.push_back(4);
	ls.push_back(5);
	ls.push_front(0);
	print(ls);
	ls.pop_back();
	print(ls);
	ls.pop_front();
	print(ls);
	List<int> lt(ls);
	std::cout << "lt";
	print(lt);
	List<int> li;
	li.push_back(0);
	lt = li;
	std::cout << "lt";
	print(lt);
	ls.clear();
	print(ls);
	print(lt);
	print(li);
	ls.swap(lt);
	print(ls);
	print(lt);
}